package com.causelist.main;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.causelist.db.CauseListDAO;
import com.causelist.exception.CauseListException;
import com.causelist.utils.CauseListConstants;

public class Main {

	static CauseListDAO causeListDAO = null;
	/**
	 * @param args
	 * @throws SQLException 
	 * @throws CauseListException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws SQLException, CauseListException, IOException {

		ApplicationContext context = new ClassPathXmlApplicationContext(CauseListConstants.BEAN_FILE);		
		causeListDAO = (CauseListDAO) context.getBean(CauseListConstants.CAUSE_LIST_DAO);
		List subscriberList = causeListDAO.retrieveSubscribersAndFiles();
		Map pretextMap;
		Set keys;
		Iterator itr;
		Map subMap = new HashMap();
		byte[] bytes = null;
		for(int i=0;i<subscriberList.size();i++)
		{
			System.out.println(subscriberList.get(i));
			pretextMap = (HashMap) subscriberList.get(i);
			subMap.put(pretextMap.get(CauseListConstants.PRETEXT_SUBS_ID),pretextMap.get(CauseListConstants.PRETEXT_SUBS_FILE));
			bytes = (byte[]) pretextMap.get(CauseListConstants.PRETEXT_SUBS_FILE);
			String testString = bytes.toString();
			FileOutputStream out = null ; 
			try 
			{
			 out = new FileOutputStream("filename.txt");  
			 out.write(bytes);   
			} 
			finally
			{   
			    out.close();
			}
			File inputFile = new File("filename.txt");	
			BufferedReader br = new BufferedReader(new FileReader(inputFile));
			String line;
			while((line = br.readLine() ) != null)
			{
				System.out.println(line);
				if(line.contains("\n"))
				{
					System.out.println("New Line");
				}
				else if(line.contains("\r"))
				{
					System.out.println("Carriage Return");
				}
			}
			CauseList cause = new CauseList();
			//cause.parseInputStream(is);
			/*while(itr.hasNext())
			{
			
				key = pretextMap.get(itr.next()).toString();
				System.out.println(pretextMap.get(itr.next()));
			}*/
		}
		
		
		
		//crawl for data and update blob
		//retrieve subscriber data and the file to be parsed.
		//parse the data and insert in to db - completed
		//retrieve data, form sms content and dispatch sms 
		
		
	}

}
